#include "magic.h"


#define FREQ 44100
#define CHANS 2

Magic::Magic(QObject *parent) :
    QObject(parent)
{
    if (!BASS_RecordInit(-1)) {
        qDebug() << "Записывающее устройство не инициализировано!";
        return;
    }
    recordsCount = 0;
}

BOOL CALLBACK RecordingCallback(HRECORD handle, const void *buffer, DWORD length, void *user)
{
    return BASS_Encode_IsActive(handle); // continue recording if encoder is alive
}

BOOL Magic::InitDevice(int device)
{
    BASS_RecordFree(); // free current device (and recording channel) if there is one
    // initalize new device
    if (!BASS_RecordInit(device)) {
        qDebug() << "Can't initialize recording device";
        return FALSE;
    }
    { // get list of inputs
        int c;
        const char *i;
        for (c=0;i=BASS_RecordGetInputName(c);c++) {
            qDebug() << i;
            if (!(BASS_RecordGetInput(c,NULL)&BASS_INPUT_OFF)) { // this one is currently "on"
                input=c;
                UpdateInputInfo();
            }
        }
    }
    return TRUE;
}

void Magic::UpdateInputInfo()
{
    char *type;
    float level;
    int it=BASS_RecordGetInput(input,&level); // get info on the input
    switch (it&BASS_INPUT_TYPE_MASK) {
        case BASS_INPUT_TYPE_DIGITAL:
            type="digital";
            break;
        case BASS_INPUT_TYPE_LINE:
            type="line-in";
            break;
        case BASS_INPUT_TYPE_MIC:
            type="microphone";
            break;
        case BASS_INPUT_TYPE_SYNTH:
            type="midi synth";
            break;
        case BASS_INPUT_TYPE_CD:
            type="analog cd";
            break;
        case BASS_INPUT_TYPE_PHONE:
            type="telephone";
            break;
        case BASS_INPUT_TYPE_SPEAKER:
            type="pc speaker";
            break;
        case BASS_INPUT_TYPE_WAVE:
            type="wave/pcm";
            break;
        case BASS_INPUT_TYPE_AUX:
            type="aux";
            break;
        case BASS_INPUT_TYPE_ANALOG:
            type="analog";
            break;
        default:
            type="undefined";
    }
    qDebug() << type;
}

void Magic::getDevices() {
    // get list of recording devices
                    BASS_DEVICEINFO di;
                    QStringList devs;
                    for (int c=0;BASS_RecordGetDeviceInfo(c,&di);c++) {
                        devs.push_back(QString::fromLocal8Bit(di.name));
                    }
                    emit devices(devs);
}

void Magic::record()
{
    // start recording (paused to add encoder first)
        rchan=BASS_RecordStart(FREQ,CHANS,BASS_RECORD_PAUSE,&RecordingCallback,0);
        if (!rchan) {
            qDebug() << "Ошибка записи!";
            return;
        }

        recordName = "rec-";
        recordName.append(QString::number(recordsCount));
        recordName.append(".wav");
        if (!(hencode=BASS_Encode_Start(rchan,recordName.toLocal8Bit().data(), BASS_ENCODE_PCM, NULL, 0))) { // start the ACM encoder
            qDebug() << "Не энкодится!";
            BASS_ChannelStop(rchan);
            rchan=0;
            return;
        }
        recordsCount++;
        BASS_ChannelPlay(rchan,TRUE); // resume recoding
}

void Magic::stop()
{
    // stop recording & encoding
        BASS_ChannelStop(rchan);
        rchan=0;
        BASS_Encode_Stop(hencode);
        qDebug() << "stop";
        emit newRecord(recordName);
}
